package com.shire.shire_interface.controller.stat;

import com.alibaba.fastjson.JSONObject;
import com.shire.shire_interface.common.bin.Response;
import com.shire.shire_interface.controller.BaseController;
import com.shire.shire_interface.model.stat.CsInviteStatExample;
import com.shire.shire_interface.service.stat.CsInviteStatService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

//@Api(tags = "统计数据")
@RestController
@RequestMapping("/stat")
public class CsInviteStatController extends BaseController {
    private static Logger logger = LoggerFactory.getLogger(CsInviteStatController.class);

    @Resource
    private CsInviteStatService inviteStatService;

//    @ApiOperation(value = "获取邀请统计，参数以json body post. accounts:帐号列表， startDate, endDate")
    @ResponseBody
    @PostMapping("/getInviteStat")
    public Response getInviteStat(@RequestBody JSONObject bodyJson, HttpSession session){
        Response response = new Response();

        try{
            if (!checkLoginUser(session)){
                response.error("请登录！");
                response.setCode(501);
                return response;
            }

            String wxAccount = bodyJson.getString("wxAccount");

            CsInviteStatExample example = new CsInviteStatExample();

            CsInviteStatExample.Criteria criteria = example.createCriteria();
            criteria.andWxaccountEqualTo(wxAccount);
            if (bodyJson.containsKey("groupNo")) {
                String groupNo = bodyJson.getString("groupNo");
                criteria.andGroupnoEqualTo(groupNo);
            }
            if (bodyJson.containsKey("groupName")) {
                String groupName = bodyJson.getString("groupName");
                criteria.andGroupnameLike("%"+groupName+"%");
            }

            if (bodyJson.containsKey("startDate")) {
                String startDate = bodyJson.getString("startDate");
                criteria.andDtGreaterThanOrEqualTo(startDate);
            }
            if (bodyJson.containsKey("endDate")) {
                String endDate = bodyJson.getString("endDate");
                criteria.andDtLessThanOrEqualTo(endDate);
            }
            example.setOrderByClause("dt desc, inviteCount desc");
            response.success("操作成功");

        }catch (Exception e){
            e.printStackTrace();
            response.error("操作失败");
        }

        return response;
    }
}
